package org.example.controller;

import org.apache.ibatis.annotations.Delete;
import org.example.pojo.Anniversary;
import org.example.pojo.Couplename;
import org.example.service.AnniversaryService;
import org.example.service.CouplenameService;
import org.example.utils.AilOssUtil;
import org.example.utils.AilOssUtilDelete;
import org.example.utils.Result;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/anniversary")
public class AnniversaryController {

    @Autowired
    private CouplenameService couplenameService;

    @Autowired
    private AnniversaryService anniversaryService;

    //添加纪恋日卡片
    @PatchMapping("/addAnniversary")
    public Result addAnniversary(String anniversaryName,
                                 String anniversaryDate) throws Exception {

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        Couplename byId = couplenameService.CoupleFindById(id);

        Integer userId = byId.getId();

        String url = null;

        anniversaryService.addAnniversary(anniversaryName, anniversaryDate, url, userId);

        return Result.success("纪念日添加成功");
    }

    //获取纪恋日列表
    @GetMapping("/list")
    public Result<List<Anniversary>> list() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        Couplename byId = couplenameService.CoupleFindById(id);

        Integer userId = byId.getId();

        List<Anniversary> list = anniversaryService.list(userId);

        return Result.success(list);

    }

    //修改纪恋日卡片
    @PutMapping("/editAnniversary")
    public Result editAnniversary(Integer id, String anniversaryName, String anniversaryDate) {

        anniversaryService.editAnniversary(id, anniversaryName, anniversaryDate);

        return Result.success("纪念日修改成功");
    }

    //修改日记图片
    @PutMapping("/editPhont")
    public Result editPhont(Integer id, MultipartFile file) throws Exception {

        String oldUrl = anniversaryService.findByUrl(id);

        //提取最后一个斜杠后面的内容
        int lastIndex = oldUrl.lastIndexOf("/");
        String fileName = oldUrl.substring(lastIndex + 1);

        //删除旧照片
        AilOssUtilDelete.main(fileName);

        //把文件上传OSS
        String originalFilename = file.getOriginalFilename();
        //保证文件名唯一，防止文件覆盖
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //添加阿里云oss存储
        String url = AilOssUtil.uploadFile(filename, file.getInputStream());

        anniversaryService.editPhon(id, url);

        return Result.success(url);

    }

    //删除纪恋日卡片
    @DeleteMapping("/deleteAnniversary")
    public Result deleteAnniversary(Integer id) {

        anniversaryService.deleteAnniversary(id);

        return Result.success("删除成功");
    }

}
